#!/usr/bin/env node

const log       = console.log,
      http      = require('http'),
      fs        = require('fs'),
      movieList = require('./data.js');

let html = fs.readFileSync('./index.html').toString('utf-8');

let list = movieList.filter(movie => movie.movieRange !== '暂无评分');

list.sort(up);

function up(x,y){
  return y.movieRange-x.movieRange;
}

log(list);

http.createServer((req,res) => {
  if(req.url === '/'){
    res.writeHead(200,{
      'Content-Type':'text/html',
      'Content-Length':Buffer.byteLength(html)    
    });
    res.end(html);
  }else if(req.url === '/getlist'){
    res.statusCode = 200;
    res.setHeader('Content-Type','text/html');
    res.end(JSON.stringify(list));
  }else{
    res.statusCode = 404;
    res.setHeader('Content-Type','text/html');
    res.end('<h1>Not Found!</h1>');
  }
    return;
}).listen(8080);
